package com.zz.yt.test.delegate.pdf;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.base.delegate.BaseDelegate;
import com.whf.android.jar.web.event.Event;
import com.whf.android.jar.web.event.EventManager;
import com.zz.yt.test.R;
import com.zz.yt.test.delegate.net.SjcDelegate;
import com.whf.android.jar.web.activity.WebUiActivity;

/**
 * pdf预览。
 */
public class DelegateView extends BaseDelegate implements AdapterView.OnItemClickListener {


    private final String[] title = {
            "activity 加载pdf",
            "delegate 加载pdf",
            "webView 处理了下载",
            "时间戳转换问题",
            "WebView与js交互调用",
            "WebView与js交互调用2",
    };

    private final BaseDelegate[] classes = {
            PdfDelegate.newInstance(),
            PdfTextDelegate.newInstance(),
            SjcDelegate.newInstance(),
            PdfJsDelegate.newInstance(),
            PdfDocDelegate.newInstance(),
    };

    @Override
    protected Object setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onBindView(@Nullable Bundle savedInstanceState,
                              @NonNull View rootView) {

        ListView idListView = findViewById(R.id.id_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                context,
                android.R.layout.simple_list_item_1,
                title);
        idListView.setAdapter(adapter);
        idListView.setOnItemClickListener(this);

        // pdf交互
        EventManager.getInstance()
                .addEvent("clickOnScan", new Event() {
                    @Override
                    public String execute(String params) {
                        ToastUtils.showShort("调研扫一扫");
                        return null;
                    }
                })
                .addEvent("clickOnImage", new Event() {
                    @Override
                    public String execute(String params) {
                        ToastUtils.showShort("选择图片");
                        return null;
                    }
                })
                .addEvent("clickOnFile", new Event() {
                    @Override
                    public String execute(String params) {
                        ToastUtils.showShort("选择文件");
                        return null;
                    }
                })
                .addEvent("clickOnLocation", new Event() {
                    @Override
                    public String execute(String params) {
                        ToastUtils.showShort("选择定位");
                        return null;
                    }
                })
                .addEvent("callPhone", new Event() {
                    @Override
                    public String execute(String params) {
                        final String phone = JsonUtils.getString(params, "phone");
                        callPhone(phone);
                        return null;
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
//            String pdf = "https://tupian.zhongzaiyuntu.com/test/d7b5f49eeb15418db5a16f825d18c3ab.pdf";
            String pdf = "file:///android_asset/jsHtml.html";
            WebUiActivity.start(context, "", pdf);
            return;
        }
        start(classes[position - 1]);
    }
}
